package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.ForceFinish;
import com.tbit.uqbike.client.service.ForceFinishService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 强制还车逻辑
 *
 * @Author: chen
 * @Description: 强制还车逻辑
 * @Date: 创建于 14:46 2023/4/18
 */
@Controller
@RequestMapping("/forceFinish")
public class ForceFinishController {

    @Autowired
    private ForceFinishService forceFinishService;
    @Autowired
    private TokenService tokenService;

    /**
     * 强制还车图片上传
     *
     * @param token
     * @param name     名称
     * @param type     类型
     * @param reason   理由
     * @param request
     * @param response
     */
    @RequestMapping("/add")
    public void add(String token, String name, Integer type, String reason, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = tokenService.getUser(token);
        try {
            Assert.notNull(reason, "-80001", "异常类型不能为空");
            Assert.notNull(type, "-110001", "申请理由不能为空");
            ForceFinish forceFinish = new ForceFinish();
            forceFinish.setUserId(user.getUserId());
            forceFinish.setPhone(user.getPhone());
            forceFinish.setType(type);
            forceFinish.setRemark(reason);
            if (name.split(";").length > 3) {
                throw new BaseException("-20025", "图片超出最大张数,请减少后在提交");
            }
            forceFinishService.insert(name, forceFinish, request);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 文件上传
     *
     * @param token
     * @param file     文件
     * @param request
     * @param response
     */
    @RequestMapping("/uploadFile")
    public void uploadFile(String token, MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = tokenService.getUser(token);
        try {
            String name = forceFinishService.uploadFile(file, request, user.getAccountId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, name);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}
